package com.iacrqq.sedaf.application.config;

import com.iacrqq.sedaf.application.config.xml.XMLApplicationParser;

public class ApplicationParserFactory 
{
	private static final String[] supportedFileType = new String[]{"xml"};
	private static final ApplicationParser[] parsers = new ApplicationParser[]{new XMLApplicationParser()};
	
	public static ApplicationParser newParser(String applicationConfig)
	{
		String extension = _get_extension_(applicationConfig);
		if(!_is_supported_(extension))
		{
			throw new RuntimeException("Unsupported config file");
		}
		
		return parsers[_index_of_(extension)];
	}
	
	private static String _get_extension_(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf(".") + 1);
	}
	
	private static int _index_of_(String extension)
	{
		int index = 0;
		for(String str : supportedFileType)
		{
			if(str.equalsIgnoreCase(extension))
			{
				return index;
			}
			
			index++;
		}
		
		return -1;
	}
	
	private static boolean _is_supported_(String extension)
	{
		for(String str : supportedFileType)
		{
			if(str.equalsIgnoreCase(extension))
			{
				return true;
			}
		}
		
		return false;
	}
}
